layui.use(['jquery', 'table', 'form', 'layer'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    //使用人id
    var createId = localStorage.getItem('createId');
    //切换不同的按钮
    $('#btns').children('button').click(function () {
        $(this).removeClass('layui-white').siblings().addClass('layui-white');
    })

    //各个结果的数量
    function getResultCount(resultId, result) {
        $.ajax({
            url: '/record/recordCount',
            type: 'post',
            dataType: 'json',
            data: {'result': result, 'type': 2},
            success: function (data) {
                resultId.text(data.data);
            }
        });
    }

    //全部数量
    getResultCount($('#allCount'), "");
    //待审核数量
    getResultCount($('#toBeReviewedCount'), 2);
    //审核通过数量
    getResultCount($('#adoptCount'), 1);
    //未通过数量
    getResultCount($('#failCount'), 3);
    //全部课程
    $('#allCourse').on('click', function () {
        getTable('');
    })
    //待审核课程
    $('#toBeReviewedCourse').on('click', function () {
        getTable(2);
    })
    //审核通过课程
    $('#adoptCourse').on('click', function () {
        getTable(1);
    })
    //未通过课程
    $('#failCourse').on('click', function () {
        getTable(3);
    })
    //加载表格数据
    getTable('');

    function getTable(result) {
        $.ajax({
            url: '/record/getRecord',
            type: 'post',
            dataType: 'json',
            data: {'type': 2, 'result': result},
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;
    //表格
    tablereload = function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            id: 'table',
            data: data,
            title: '课程审核列表',
            toolbar: '#toolbar',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: '首页',
                last: '尾页'
            },
            cols: [
                [
                    {type: 'checkbox'},
                    {
                        field: '', title: '序号', align: 'center', templet: function (data) {
                            return data.LAY_INDEX;
                        }
                    },
                    {field: 'courseId', title: '课程编号', align: 'center'},
                    {
                        field: 'url', title: '课程图片', align: 'center', templet: function (data) {
                            return "<img src='http://" + data.url + "' style='width: 100px;height: 100px'>";
                        }
                    },
                    {field: 'courseName', title: '课程名称', align: 'center'},
                    {
                        field: 'courseNprice', title: '课程价格', align: 'center', templet: function (data) {
                            return "￥" + data.courseNprice;
                        }
                    },
                    {field: 'title', title: '课程分类', align: 'center'},
                    {field: 'trueName', title: '上传人员', align: 'center'},
                    {
                        field: 'result', title: '审核状态', align: 'center', templet: function (data) {
                            if (data.result == 2) {
                                return "<p>待审核</p>";
                            } else if (data.result == 1) {
                                return "<p>审核通过</p>";
                            } else {
                                return "<p>审核未通过</p>";
                            }
                        }
                    },
                    {
                        field: '', title: '操作', align: 'center', templet: function (data) {
                            if (data.result == 2) {
                                return "<div>" +
                                    "<a href='javascript:getCourseById(" + data.courseId + ");' style='color: #2F4056;margin-right: 10px'>查看</a>" +
                                    "<a href='javascript:updateRecord(\"" + data.id + "\",\"" + data.courseName + "\");' style='color: #2F4056'>审核</a>" +
                                    "</div>";
                            }
                            return "<div>" +
                                "<a href='javascript:getCourseById(" + data.courseId + ");' style='color: #2F4056;'>查看</a>" +
                                "</div>";

                        }
                    }
                ]
            ],
        });
    }
    //查看课程详细信息
    getCourseById = function getCourseById(id) {
        $.ajax({
            url: '/course/getCourseById',
            type: 'post',
            dataType: 'json',
            data: {'courseId': id, 'userId': createId},
            success: function (data) {
                viewDetails(data.data);
            }
        });
    }

    function viewDetails(data) {
        layer.open({
            type: 2,
            title: '查看课程详细信息',
            shade: 0,
            area: ['1000px', '500px'],
            offset: 't',
            maxmin: true,
            btn: ['关闭'],
            content: '/course/course-see.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                var url = "http://" + data.url;
                obj.find('#url').attr("src", url);
                var courseUrl = "http://" + data.courseUrl;
                obj.find('#courseUrl').attr("src", courseUrl);
                obj.find('input[name=courseName]').val(data.courseName);
                obj.find('input[name=createDate]').val(data.createDate);
                obj.find('input[name=trueName]').val(data.trueName);
                obj.find('input[name=courseNprice]').val(data.courseNprice);
                obj.find('input[name=coursePrimary]').val(data.coursePrimary);
                obj.find('input[name=title]').val(data.title);
                obj.find('input[name=likeNum]').val(data.likeNum);
                obj.find('input[name=collectNum]').val(data.collectNum);
                obj.find('input[name=commentNum]').val(data.commentNum);
                obj.find('#courseIntro').val(data.courseIntro);
            }
        });
    }

    //审核
    updateRecord = function updateRecord(id, courseName) {
        layer.open({
            type: 2,
            title: '课程审核',
            maxmin: true,
            shade: 0,
            area: ['450px', '350px'],
            content: '/examine/course-examine-detailed.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('input[name=id]').val(id);
                obj.find('input[name=courseName]').val(courseName);
            }
        });
    }
    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/record/getRecord?type=2',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data);
            }
        });
    })
    //重置
    $('#reset').on('click', function () {
        $('input[name=courseName]').val('');
        $('select[name=classId]').val('');
        $('input[name=courseNprice]').val('');
        form.render();
        getTable('');
    })
    //下拉框
    dropDownBox();

    function dropDownBox() {
        $.ajax({
            url: "/courseClass/getCourseClass",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                $('#classId').append("<option value=''>选择分类类型</option>");
                for (var i = 0; i < d.length - 1; i++) {
                    var id = d[i].categroyId;
                    var name = d[i].title;
                    $('#classId').append("<option value='" + id + "'>" + name + "</option>")
                }
                form.render('select');
            }
        });
    }

    //刷新
    $('#refresh').on('click', function () {
        location.reload();
    })
    //返回
    $('#goBack').on('click', function () {
        //跳转课程列表
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    })
});